package zzhyc.keystore

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTDrawFeedAd
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot
import com.bytedance.sdk.openadsdk.mediation.ad.MediationExpressRenderListener
import zzhyc.keystore.databinding.ActivityDrawBinding

private const val TAG = "DrawActivity"

class DrawActivity : AppCompatActivity() {

    private val binding by lazy { ActivityDrawBinding.inflate(layoutInflater) }
    private var drawdAd: TTDrawFeedAd? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.drawTvBack.setOnClickListener { finish() }
        initDrawAdslot()
    }

    //构造Draw Adslot
    private fun initDrawAdslot() {
        Log.i(TAG, "加载 Draw 信息流")
        binding.drawFl.removeAllViews()
        /**
         * 注：
         *  1:单位为px
         *  2:如果是Draw自渲染广告，设置广告图片期望的图片宽高 ，不能为0
         *  2:如果是Draw模板广告，宽度设置为希望的宽度，高度设置为0(0为高度选择自适应参数)
         */
        val drawSlot = AdSlot.Builder()
            .setCodeId(ConstantsKey.DRAW_APP_ID) //广告位ID
            .setImageAcceptedSize(UIUtils.getScreenWidthInPx(this), 0)
            .setAdCount(1)  //请求广告数量为1到3条 （优先采用平台配置的数量）
            // 聚合广告请求配置
            .setMediationAdSlot(MediationAdSlot.Builder().setMuted(true).build()).build()

        val adNativeLoader = TTAdSdk.getAdManager().createAdNative(this)
        adNativeLoader.loadDrawFeedAd(drawSlot, object : TTAdNative.DrawFeedAdListener {
            override fun onError(code: Int, message: String?) { //广告加载失败
                Log.i(TAG, "Draw信息流/onError-- ${code}， msg-- ${message}")
            }

            override fun onDrawFeedAdLoad(ads: MutableList<TTDrawFeedAd>?) {    //广告加载成功
                Log.i(TAG, "Draw信息流/adsSize-- ${ads?.size}")
                ads?.let {
                    if (it.size > 0) {
                        drawdAd = ads[0]
                        drawdAd?.let { drawFeedAd ->
                            val manager = drawFeedAd.mediationManager
                            val isExpressAd = manager.isExpress
                            Log.i(TAG, "Draw信息流/模板广告-- ${isExpressAd}")
                            if (isExpressAd) {
                                drawFeedAd.setExpressRenderListener(object :
                                    MediationExpressRenderListener {
                                    override fun onRenderSuccess(
                                        p0: View?, p1: Float, p2: Float, p3: Boolean
                                    ) {
                                        Log.i(TAG, "Draw信息流/onRenderSuccess")
                                        drawFeedAd.adView?.let { view ->
                                            UIUtils.removeFromParent(view)
                                            binding.drawFl.removeAllViews()
                                            binding.drawFl.addView(view)

                                            val ecpmInfo = manager.showEcpm
                                            val ecpm = ecpmInfo.ecpm
                                            Log.i(
                                                TAG,
                                                "Draw信息流/广告价格-- ${ecpm}, 多阶底价标签解析失败原因 ErrorMsg: ${ecpmInfo.errorMsg}"
                                            )
                                        }
                                    }

                                    override fun onRenderFail(p0: View?, p1: String?, p2: Int) {
                                        Log.i(TAG, "Draw信息流/onRenderFail-- ${p2}，msg-- ${p1}")
                                    }

                                    override fun onAdClick() {

                                    }

                                    override fun onAdShow() {
                                        Log.i(TAG, "Draw信息流/onAdShow")
                                    }
                                })
                                drawFeedAd.render()
                            }
                        }
                    }
                }
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        drawdAd?.destroy()
    }
}